<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>计费标准管理</title>
    <link rel="stylesheet" type="text/css" href="/themes/default/easyui.css" id="swicth-style">
    <link rel="stylesheet" type="text/css" href="/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/css/common.css">
    <link rel="stylesheet" type="text/css" href="/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/themes/color.css">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/jquery.cookie.js"></script>
    <script type="text/javascript" src="/js/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/common.js"></script>
</head>

<body class="easyui-layout" style="padding:5px;">
<fieldset>
    <legend>信息查询</legend>
    <form id="ffSearch" method="post">
        <div id="searchBar">
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <th>计费标准名称：</th>
                    <td style="width:120px">
                        <input name="name" class="easyui-textbox" type="text" style="width:100px">
                    </td>

                    <th>计费标准状态：</th>
                    <td style="width:120px">
                        <select id="statusSelect" name="status" class="easyui-combobox" data-options="onSelect: function(rec){onSearch()}" style="width:100px">
                            <option value="0">全部</option>
                            <option value="1">启用</option>
                            <option value="2">禁用</option>
                        </select>
                    </td>
                    <td colspan="2">
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="onSearch()">查询</a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="onClear()">重置</a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</fieldset>

<table id="dg"> </table>

<div id="addOrEdit" class="easyui-window" title="添加充计费标准" data-options="closed:true,iconCls:'icon-save',footer:'#ft'" style="width:450px;height:300px;padding:10px;">
    <form id="ff" method="post">
        <table class="table-blue">
            <tr>
                <th>计费标准名称：</th>
                <td><input type="hidden" name="id"/><input class="easyui-validatebox textbox" type="text" name="name" data-options="required:true, validType:'maxLength[50]'"/></td>
            </tr>
            <tr>
                <th>功率下限：</th>
                <td><input class="easyui-validatebox textbox" type="text" id="powerLower" name="powerLower" data-options="required:true, validType:'maxLength[20]'"/><span>瓦</span></td>
            </tr>
            <tr>
                <th>功率上限：</th>
                <td><input class="easyui-validatebox textbox" type="text" id="powerUpper" name="powerUpper" data-options="required:true, validType:'maxLength[20]'"/><span>瓦</span></td>
            </tr>
            <tr>
                <th>单价：</th>
                <td><input class="easyui-validatebox textbox" type="text" id="unitRice" name="unitRice" data-options="required:true, validType:'maxLength[50]'"/><span>元</span></td>
            </tr>
            <tr>
                <th>单位：</th>
                <td>
                    <select id="unitTypeSelect" name="unitType" class="easyui-combobox"  style="width:100px">
                        <option value="1">元/小时</option>
                        <option value="2">元/度</option>
                    </select>
                </td>
            </tr>
        </table>
    </form>
    <div id="ft" style="padding:5px;text-align: right;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="submitForm()" style="width:80px">Ok</a> &nbsp;&nbsp;
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="closeWindow()" style="width:80px">Cancel</a>
    </div>
</div>



<script type="text/javascript">
    $(function(){
        var dg = $('#dg').datagrid();
        dg.datagrid({
            url:'/v1/api0/equipmentConfig/chargeStandardList',
            columns:[[
                {field:'ck',checkbox:true},
                {field:'id',hidden:true,title:'计费标准ID',width:100},
                {field:'name',title:'计费标准名称',width:100},
                {field:'powerLower',title:'功率下限',width:100},
                {field:'powerUpper',title:'功率上限',width:100},
                {field:'unitRice',title:'单价',width:100},
                {field:'unitType',title:'单位',width:100,formatter: function(value,row,index){
                    switch(value) {
                        case 1: return "元/小时";
                        case 2: return "元/度";
                    }
                }},
                {field:'createTime',title:'创建时间',width:100,formatter: formatDateTime},
                {field:'status',title:'状态',width:100,formatter: function(value,row,index){
                    switch(value) {
                        case 1: return "启用";
                        case 2: return "禁用";
                    }
                }},
                {field:'action',title:'操作',width:100,formatter: function(value,row,index){
                    if(row.status == 1){
                        return  '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="disable(\''+row.id+'\')">禁用</a>';
                        // '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="alert(\'未实现\');">...</a>'+' | '+
                            //'<a href="javascript:void(0)" class="easyui-linkbutton" onclick="alert(\'未实现\');">...</a>'+' | '+

                    }else{
                        return  '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="enable(\''+row.id+'\')">启用</a>';
                        // '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="alert(\'未实现\');">...</a>'+' | '+
                             //'<a href="javascript:void(0)" class="easyui-linkbutton" onclick="alert(\'未实现\');">...</a>'+' | '+

                    }
                }}
            ]],
            title:'计费标准管理',
            pagination: true,
            iconCls: 'icon-application_view_detail',
            singleSelect: false,
            toolbar: [{
                id: 'btnAdd',
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    add();
                }
            }, '-', {
                id: 'btnEdit',
                text: '修改',
                iconCls: 'icon-edit',
                handler: function () {
                    edit();
                }
            }, '-', {
                id: 'btnDelete',
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    del();
                }
            }, '-', {
                id: 'btnReload',
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    $("#dg").datagrid("reload");
                }
            }]
        });

        var heightMargin = $("#searchBar").height() + 45;
        $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        $(window).resize(function() {
            $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        });
    });

    function onSearch(){
        var queryParams = $('#ffSearch').serializeObject();

        $('#dg').datagrid({
            url:'/v1/api0/equipmentConfig/chargeStandardList',
            queryParams: queryParams
        });
    }

    function onClear() {
        $('#ffSearch').form('clear');
        $('#statusSelect').combobox('setValue', '0');
        onSearch();
    }

    function add() {
        $('#ff').form('clear');
        $("#ff input[name='id']").val("");
        $('#addOrEdit').window({title:'添加计费标准'});
        $("#unitTypeSelect").combobox("setValue","1");
        $('#addOrEdit').window('open');
    }

    function edit() {
        $('#ff').form('clear');
        var row = $('#dg').datagrid('getSelected');
        if (!row){
            top.window.showMessage("请选择计费标准");
            return;
        }
        $('#ff').form('load',row);
        $("#ff input[name='id']").val(row.id);
        $('body').data('ff-serialize', $('#ff').serialize());
        $('#addOrEdit').window({title:'编辑计费标准'});
        $('#addOrEdit').window('open');
    }

    function submitForm(){
        var powerLower = $("#powerLower").val();
        var powerUpper = $("#powerUpper").val();
        var unitRice = $("#unitRice").val();
        if(!checkNumber(powerLower)){
            top.window.showMessage("请输入合法的功率下限");
                return;
        }
        if(!checkNumber(powerUpper)){
            top.window.showMessage("请输入合法的功率上限");
                return;
        }
        if(!checkNumber(unitRice)){
            top.window.showMessage("请输入合法的单价");
                return;
        }
        var id = $("#ff input[name='id']").val();
        var url = id.length ? "/v1/api0/equipmentConfig/editChargeStandard" : "/v1/api0/equipmentConfig/addChargeStandard";
        var data = $('#ff').serialize();
        if(data == $('body').data('ff-serialize')) {
            top.window.showMessage("数据没有改动");
            return;
        }
        $.ajax({
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                if (!$('#ff').form('validate')){
                    return false;
                }
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        });
    }

    function closeWindow(){
        $('#addOrEdit').window('close');
        $('#bind').window('close');
    }

    function del() {
        var ids = [];
        var rows = $('#dg').datagrid('getSelections');
        if(!rows.length) {
            top.window.showMessage("请选择计费标准");
            return;
        }
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }

        top.window.$.messager.confirm('确认', '您确定要删除选择的计费标准吗？', function(r){
            if (r){
                var url = "/v1/api0/equipmentConfig/batchDelChargeStandard";
                var data = {ids: ids.join(',')};
                $.ajax(ajaxOption(url, data));
            }
        });
    }

    /* function disable(id){
         alert("============"+id);
         var url = "/v1/api0/equipmentConfig/disableTerminal";
         var data = {id: id};
         $.ajax(ajaxOption(url, data));
     }*/

    function disable(id) {
    top.window.$.messager.confirm('确认', '您确定要禁用该收费标准吗？', function(r){
            if (r){
                 var url = "/v1/api0/equipmentConfig/disableChargeStandard";
                var data = {id: id};
                $.ajax(ajaxOption(url, data));
            }
        });

    }


    function enable(id) {
     top.window.$.messager.confirm('确认', '您确定要启用该收费标准吗？', function(r){
            if (r){
                 var url = "/v1/api0/equipmentConfig/enableChargeStandard";
                var data = {id: id};
             $.ajax(ajaxOption(url, data));
            }
        });

    }

    function ajaxOption(url, data) {
        return {
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        }
    }

    function checkNumber(input) {
 　　var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/
　　if (!re.test(input)) {
 　　　　return false;
 　　}else{
            return true;
        }
 }
</script>
</body>
</html>
